/**
  ******************************************************************************
  * @file    adc.c
  * @brief   This file provides code for the configuration
  *          of the ADC instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */


#include "adc.h"



extern uint16_t Ref_Val;
extern eDeviceMode gDeviceRunMode;
extern uint16_t	Water_Level_Range;


ADC_HandleTypeDef hadc;


void MX_ADC_Init(void)
{

  

  

  ADC_ChannelConfTypeDef sConfig = {0};

  

  
  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */
  hadc.Instance = ADC1;
  hadc.Init.OversamplingMode = DISABLE;
  hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
  hadc.Init.Resolution = ADC_RESOLUTION_12B;
  hadc.Init.SamplingTime = ADC_SAMPLETIME_39CYCLES_5;
  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ContinuousConvMode = DISABLE;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc.Init.DMAContinuousRequests = DISABLE;
  hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc.Init.LowPowerAutoWait = DISABLE;
  hadc.Init.LowPowerFrequencyMode = DISABLE;
  hadc.Init.LowPowerAutoPowerOff = DISABLE;
  if (HAL_ADC_Init(&hadc) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure for the selected ADC regular channel to be converted.
  */
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure for the selected ADC regular channel to be converted.
  */
  sConfig.Channel = ADC_CHANNEL_1;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure for the selected ADC regular channel to be converted.
  */
  sConfig.Channel = ADC_CHANNEL_VREFINT;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  

  

}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(adcHandle->Instance==ADC1)
  {
  

  
    
    __HAL_RCC_ADC1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**ADC GPIO Configuration
    PA0     ------> ADC_IN0
    PA1     ------> ADC_IN1
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  

  
  }
}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
{

  if(adcHandle->Instance==ADC1)
  {
  

  
    
    __HAL_RCC_ADC1_CLK_DISABLE();

    /**ADC GPIO Configuration
    PA0     ------> ADC_IN0
    PA1     ------> ADC_IN1
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1);

  

  
  }
}




uint16_t ADC_Vref_Data(void) 	
{
	uint16_t adc_data;
	ADC_ChannelConfTypeDef sConfig = {0};
	while (__HAL_PWR_GET_FLAG(PWR_FLAG_VREFINTRDY) == RESET) 
		{};
	__HAL_RCC_ADC1_CLK_ENABLE();
	HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED );
	sConfig.Channel = ADC_CHANNEL_MASK;
	sConfig.Rank = ADC_RANK_NONE; 
	HAL_ADC_ConfigChannel( &hadc, &sConfig);

	
	sConfig.Channel = ADC_CHANNEL_VREFINT;
	sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
	HAL_ADC_ConfigChannel( &hadc, &sConfig);
	
	HAL_ADC_Start(&hadc);

	
	HAL_ADC_PollForConversion( &hadc, 50 );
	adc_data = HAL_ADC_GetValue(&hadc);
	__HAL_ADC_DISABLE(&hadc);

  __HAL_RCC_ADC1_CLK_DISABLE();
	return adc_data;
}


uint16_t ADC_channel0_Data(void)
{
	uint16_t adc_data;
	ADC_ChannelConfTypeDef sConfig = {0};
	
	DC_5_0V_ON;   
	HAL_Delay(150);
	
	while (__HAL_PWR_GET_FLAG(PWR_FLAG_VREFINTRDY) == RESET) {};
	__HAL_RCC_ADC1_CLK_ENABLE();
	HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED );
	sConfig.Channel = ADC_CHANNEL_MASK;
	sConfig.Rank = ADC_RANK_NONE; 
	HAL_ADC_ConfigChannel( &hadc, &sConfig);

	
	sConfig.Channel = ADC_CHANNEL_0;
	sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
	HAL_ADC_ConfigChannel( &hadc, &sConfig);
	
	HAL_ADC_Start(&hadc);

	
	HAL_ADC_PollForConversion( &hadc, 50 );
	adc_data = HAL_ADC_GetValue(&hadc);
	__HAL_ADC_DISABLE(&hadc);

  __HAL_RCC_ADC1_CLK_DISABLE();
		
	DC_5_0V_OFF;
		
	return adc_data;
}

uint16_t ADC_channel1_Data(void)
{
	uint16_t adc_data;
	ADC_ChannelConfTypeDef sConfig = {0};
	
		DC_DC1_ON;
		HAL_Delay(5);
		DC_DC2_ON;
		HAL_Delay(250);
		
		if(Water_Level_Range>5000)
		HAL_Delay(250);

	while (__HAL_PWR_GET_FLAG(PWR_FLAG_VREFINTRDY) == RESET) {};
	__HAL_RCC_ADC1_CLK_ENABLE();
	HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED );
	sConfig.Channel = ADC_CHANNEL_MASK;
	sConfig.Rank = ADC_RANK_NONE; 
	HAL_ADC_ConfigChannel( &hadc, &sConfig);

	
	sConfig.Channel = ADC_CHANNEL_1;
	sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
	HAL_ADC_ConfigChannel( &hadc, &sConfig);
	
	HAL_ADC_Start(&hadc);

	
	HAL_ADC_PollForConversion( &hadc, 50 );
	adc_data = HAL_ADC_GetValue(&hadc);
	__HAL_ADC_DISABLE(&hadc);

  __HAL_RCC_ADC1_CLK_DISABLE();
		
	DC_DC2_OFF;
	HAL_Delay(1);
	DC_DC1_OFF;
	
	return adc_data;
}






